package com.hrsaas.ext.spider.core.url;

import com.hrsaas.ext.spider.util.Assert;

/**
 * <p>
 * 功能：请求协议,目前只包含 http / https双协议
 * <p>
 * @author wangxiaoliang@kanzhun.com
 *
 * 2018年8月1日
 */
public enum Protocol {
	
    HTTP,
    HTTPS;

    public String getName() {
        return name().toLowerCase();
    }

    public int getDefaultPort() {
        switch(this) {
            case HTTP :
                return 80;
            case HTTPS :
                return 443;
            default :
                return -1;
        }
    }

    public boolean isSecure() {
        return this == HTTPS;
    }

    public boolean match(String protocol) {
        Assert.notNull("protocol", protocol);
        return name().compareToIgnoreCase(protocol) == 0;
    }

    public static Protocol forName (String name) {
        for (Protocol p : values()) {
            if (p.match(name)) {
                return p;
            }
        }
        throw new UnsupportedOperationException(name);
    }

    @Override
    public String toString() {
        return name().toLowerCase();
    }

}
